import { createServer, logger, Command, ModeOptions, LogLevelOptions, UserStore } from '..'
import { app_base, app_system, app_marked } from "./apps";
import path from "node:path";

const command = new Command('f2e-server3')
    .option('-m, --mode <mode>', 'server mode: dev, build or prod', 'dev', ModeOptions)
    .option('-g, --gzip <gzip>', 'enable gzip', true)
    .option('-r, --root <root>', 'root', process.cwd())
    .option('-l, --level <level>', 'log level: DEBUG, INFO, LOG, WARN, ERROR', 'DEBUG', LogLevelOptions)
    .action(async (options) => {
        const { mode, gzip, level, root } = options
        logger.setLevel(level)
        let i = 0;
        createServer({
            root, mode, gzip,
            ssl: {
                passphrase: 'x509',
                key_file_name: path.join(root, './test/ssl/private.pem'),
                cert_file_name: path.join(root, './test/ssl/csr.crt'),
            },
            mimeTypes: {
                'ts': 'text/plain',
                'hbs': 'text/plain',
            },
            buildFilter: (pathname) => {
                return /^(src|templates|test($|\/index1?\.html|\/start)|README|package|$)/.test(pathname)
            },
            outputFilter: (pathname) => {
                return /^(static|test($|\/index1?\.html|\/start)|README|package|$)/.test(pathname)
            },
            watchFilter: (pathname) => {
                return /^(test|README|package|$)/.test(pathname)
            },
            esbuild: {
                reg_inject: /\.html$/,
            },
            less: {
                entryPoints: [{
                    in: 'test/app/app.less',
                    out: 'static/app.css',
                }],
            },
            postcss: {
                // 仅支持单个
                entryPoints: {
                    in: './test/main.css',
                    out: 'static/main.css',
                },
                plugins: [
                    require('@tailwindcss/postcss')({
                        optimize: mode === 'build',
                    }),
                ],
            },
            alias: {
                'static/monokai-sublime.css': 'node_modules/highlight.js/styles/monokai-sublime.css',
            },
            include: {
                entryPoints: ['test/app/pages/include.html'],
                recursive: true,
            },
            try_files: 'test/index.html',
            auth: mode === 'build' ? false : {
                redirect: true,
                store: new UserStore('.f2e_cache/auth.db'),
            },
            middlewares: [
                app_base,
                app_system,
                app_marked,
            ],

            livereload: {
                reg_inject: /\.(html|md)$/,
            },
        })
    })
command.parse(process.argv)